package com.mall.coupon.server.service.impl;

import com.mall.common.security.utils.SecurityUtils;
import com.mall.coupon.common.domain.domain.Coupon;
import com.mall.coupon.common.domain.domain.UserCouponMiddle;
import com.mall.coupon.server.mapper.CouponMapper;
import com.mall.coupon.server.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CouponServiceImpl
 * @Description 描述
 * @Author Qiu Feng
 * @Date 2025/1/13 14:41
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    private CouponMapper couponMapper;
//    @Autowired
//    private PmsSkuInfoRemoteService pmsSkuInfoRemoteService;

    @Override
    public void userCouponAdd(Long couponId) {
        //获取当前登录人
        Long userId = SecurityUtils.getUserId();
//        Long userId = 1L;
        //根据couponId查询优惠券信息
        Coupon coupon = couponMapper.selectCouponById(couponId);
        if (null == coupon){
            throw new RuntimeException("优惠券不存在");
        }
//        UserCouponMiddle userCouponMiddles = couponMapper.selectUserIdAndCouponId(userId, couponId);
//        if (null != userCouponMiddles){
//            throw new RuntimeException("优惠券不能重复领取");
//        }
        UserCouponMiddle userCouponMiddle = new UserCouponMiddle();
        userCouponMiddle.setCouponId(Long.valueOf(couponId));

        userCouponMiddle.setUserId(userId);
        userCouponMiddle.setCouponId(Long.valueOf(couponId));
        userCouponMiddle.setCouponName(coupon.getCouponName());
        userCouponMiddle.setCouponType(0);
        userCouponMiddle.setCouponStatus(0);

        couponMapper.userCouponAdd(userCouponMiddle);
    }

    @Override
    public List<UserCouponMiddle> userCouponMiddleList() {
        return couponMapper.userCouponMiddleList();
    }

    @Override
    public void updateUserCouponMiddleStatus(Integer id) {
        couponMapper.updateUserCouponMiddleStatus(id);
    }

    @Override
    public List<Coupon> settlementCouponList() {
        List<Coupon> coupons = couponMapper.settlementCouponList();
        return coupons;
    }

//    @Override
//    public List<Coupon> filteredCoupons(Long skuId) {
//        //获取用户ID
//        //Long userId = SecurityUtils.getUserId();
//        Long userId = 1L;
//        Result info = pmsSkuInfoRemoteService.getInfo(skuId);
//        Object data = info.getData();
//        if (null == data){
//            throw new RuntimeException("商品不存在");
//        }
//        return couponMapper.settlementCouponList();
//    }

//    @Override
//    public List<UserCouponMiddle> filteredCoupon(List<UserCouponMiddle> userCouponMiddles, List<SkuCouponMiddle> skuCouponMiddles, Long skuId) {
//        return userCouponMiddles.stream().filter(userCouponMiddle -> {
//            if (userCouponMiddle.getCouponType() != 2){
//                return true;
//            }
//            boolean hasMatchingSku = skuCouponMiddles.stream()
//                    .anyMatch(skuCouponMiddle ->
//                            skuCouponMiddle.getCouponId().equals(userCouponMiddle.getCouponId()) &&
//                            skuCouponMiddle.getStatus() == 1);
//            return hasMatchingSku;
//        }).collect(Collectors.toList());
//    }

//    @Override
//    public List<UserCoupon> filterUserCoupons(List<UserCoupon> userCoupons, List<CouponSku> couponSkuList) {
//        return userCoupons.stream().filter(userCoupon -> {
//            // 如果优惠券类型不是2，直接保留
//            if (userCoupon.getCouponType() != 2) {
//                return true;
//            }
//            // 对于类型为2的优惠券，检查是否存在对应的SKU
//            boolean hasMatchingSku = couponSkuList.stream().anyMatch(couponSku -> couponSku.getCouponId()
//            .equals(userCoupon.getCouponId()) && couponSku.getStatus() == 1
//                    // 假设1表示有效状态
//            );
//            return hasMatchingSku;
//        }).collect(Collectors.toList());
//    }
}
